package com.adingxiong.cft.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * @ClassName ProductTest
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/3/19 15:39
 **/
public class ProductTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties props = KafkaConfig.initConfig();
        Producer<String,String> producer = new KafkaProducer<>(props);

        /*// 发送消息
        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<>("test",i+"", i+"") ,(metadata,exception) -> {
                if(exception == null) {
                    System.out.println("消息发送成功->" + metadata.offset());
                }else {
                    exception.printStackTrace();
                }
            });
        }*/

        for (int i = 0; i < 100; i++) {
            RecordMetadata metadata = producer.send(
                    new ProducerRecord<>("test", Integer.toString(i), Integer.toString(i))
            ).get();
            System.out.println(metadata.offset());
        }

        producer.close();
    }


}
